"""
Case Type   : Compress
Case Name   : quality压缩等级为1时，压缩成功
Create At   : 2024/11/12
Owner       : xiaqi14
Description :
              1.使用兼容A库创建压缩插件
              drop database if exists test_dcompress_01;
              create database if not exists test_dcompress_01 db_compatibility = 'A';
              create extension gms_compress;
              2.设置LZ_COMPRESS_OPEN入参dst有值，quality压缩等级为1
              DECLARE
                content BLOB[5];
                y_handle int[5];
              BEGIN
                content[0] := '123';
                y_handle[0] := GMS_COMPRESS.LZ_COMPRESS_OPEN(content[0],1);
              END;
              3.清理环境
Expect      :
              1.成功
              2.成功
              3.成功
History     :
"""

import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class Timescaledb(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info('==Opengauss_Function_Compress_Case0011开始==')
        self.Constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.db_user_node = Node(node='PrimaryDbUser')
        self.db_name = "test_compress_11"
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status or "Degraded" in status)

    def test_add_dimension(self):
        text = '----step1: 使用兼容A库创建压缩插件 expect: 成功----'
        self.log.info(text)
        self.log.info("检验创建兼容A库功能是否正常")
        drop_database_sql = "drop database if exists " + self.db_name + ";"
        drop_database_result = self.primary_sh.execut_db_sql(drop_database_sql)
        self.assertIn(self.Constant.DROP_DATABASE_SUCCESS, drop_database_result, "执行失败")
        create_database_sql = "create database " + self.db_name + " dbcompatibility = 'A';"
        create_database_result = self.primary_sh.execut_db_sql(create_database_sql)
        self.assertIn(self.Constant.CREATE_DATABASE_SUCCESS, create_database_result, "执行失败")
        show_compatibility_sql = "show sql_compatibility;"
        show_compatibility_result = self.primary_sh.execut_db_sql(show_compatibility_sql, dbname=self.db_name)
        self.assertIn('A', show_compatibility_result, "执行失败")
        self.log.info("使用兼容A库创建压缩插件")
        drop_extension_sql = "drop extension if exists gms_compress;"
        drop_extension_result = self.primary_sh.execut_db_sql(drop_extension_sql, dbname=self.db_name)
        self.assertIn(self.Constant.drop_extension_success, drop_extension_result)
        create_extension_sql = "create extension gms_compress;"
        create_extension_result = self.primary_sh.execut_db_sql(create_extension_sql, dbname=self.db_name)
        self.assertIn(self.Constant.extension_create_success, create_extension_result, "执行失败")

        text = '----step2: 设置LZ_COMPRESS_OPEN入参dst有值，quality压缩等级为1 expect:成功----'
        self.log.info(text)
        test_sql = "DECLARE " \
                   + "content BLOB[5]; " \
                   + "y_handle int[5]; " \
                   + "BEGIN " \
                   + "content[0] := '123'; " \
                   + "y_handle[0] := GMS_COMPRESS.LZ_COMPRESS_OPEN(content[0],1); " \
                   + "END; "
        select_result = self.primary_sh.execut_db_sql(test_sql, dbname=self.db_name)
        self.assertNotIn('ERROR', select_result, "执行失败")

    def tearDown(self):
        text = '----step3: 清理环境 expect: 成功----'
        self.log.info(text)
        drop_extension_sql = "drop extension if exists gms_compress;"
        drop_extension_result = self.primary_sh.execut_db_sql(drop_extension_sql, dbname=self.db_name)
        self.assertIn(self.Constant.drop_extension_success, drop_extension_result)
        drop_database_sql = "drop database if exists " + self.db_name + ";"
        drop_database_result = self.primary_sh.execut_db_sql(drop_database_sql)
        self.assertIn(self.Constant.DROP_DATABASE_SUCCESS, drop_database_result, "执行失败")
        self.log.info('==Opengauss_Function_Compress_Case0011完成==')
